package net.realsee.service.wcs.work.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.work.WcsOrderStatusHist;
import net.realsee.domain.wcs.work.bo.WcsOrderStatusHistBo;
import net.realsee.domain.wcs.work.vo.WcsOrderStatusHistVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.work.WcsOrderStatusHistMapper;
import net.realsee.service.wcs.work.IWcsOrderStatusHistService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 单据状态历史Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsOrderStatusHistServiceImpl extends ServiceImpl<WcsOrderStatusHistMapper, WcsOrderStatusHist> implements IWcsOrderStatusHistService {

    private final WcsOrderStatusHistMapper baseMapper;

    /**
     * 查询单据状态历史
     */
    @Override
    public WcsOrderStatusHistVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询单据状态历史列表
     */
    @Override
    public TableDataInfo<WcsOrderStatusHistVo> selectVoPage(WcsOrderStatusHistBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsOrderStatusHist> lqw = buildVoQueryWrapper(bo);
        Page<WcsOrderStatusHistVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询单据状态历史列表
     */
    @Override
    public List<WcsOrderStatusHistVo> selectVoList(WcsOrderStatusHistBo bo) {
        LambdaQueryWrapper<WcsOrderStatusHist> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsOrderStatusHist> buildVoQueryWrapper(WcsOrderStatusHistBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsOrderStatusHist> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsOrderStatusHist::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), WcsOrderStatusHist::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderType() != null, WcsOrderStatusHist::getOrderType, bo.getOrderType());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskType()), WcsOrderStatusHist::getTaskType, bo.getTaskType());
        lqw.eq(bo.getOrderLineNo() != null, WcsOrderStatusHist::getOrderLineNo, bo.getOrderLineNo());
        lqw.eq(bo.getFromOrderStatus() != null, WcsOrderStatusHist::getFromOrderStatus, bo.getFromOrderStatus());
        lqw.eq(bo.getToOrderStatus() != null, WcsOrderStatusHist::getToOrderStatus, bo.getToOrderStatus());
        lqw.eq(bo.getFromProgressStatus() != null, WcsOrderStatusHist::getFromProgressStatus, bo.getFromProgressStatus());
        lqw.eq(bo.getToProgressStatus() != null, WcsOrderStatusHist::getToProgressStatus, bo.getToProgressStatus());
        lqw.eq(bo.getChangeStatusTime() != null, WcsOrderStatusHist::getChangeStatusTime, bo.getChangeStatusTime());
        lqw.eq(StringUtils.isNotBlank(bo.getChangeStatusUser()), WcsOrderStatusHist::getChangeStatusUser, bo.getChangeStatusUser());
        lqw.eq(StringUtils.isNotBlank(bo.getComment()), WcsOrderStatusHist::getComment, bo.getComment());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationCode()), WcsOrderStatusHist::getOperationCode, bo.getOperationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsOrderStatusHist::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsOrderStatusHist::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsOrderStatusHist::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsOrderStatusHist::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsOrderStatusHist::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsOrderStatusHist::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsOrderStatusHist::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsOrderStatusHist::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsOrderStatusHist::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsOrderStatusHist::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsOrderStatusHist::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsOrderStatusHist::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsOrderStatusHist::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增单据状态历史
     */
    @Override
    public Boolean insertByBo(WcsOrderStatusHistBo bo) {
        WcsOrderStatusHist add = BeanUtil.toBean(bo, WcsOrderStatusHist.class);
        return this.save(add);
    }

    /**
     * 修改单据状态历史
     */
    @Override
    public Boolean updateByBo(WcsOrderStatusHistBo bo) {
        WcsOrderStatusHist update = BeanUtil.toBean(bo, WcsOrderStatusHist.class);
        return this.updateById(update);
    }

    /**
     * 批量删除单据状态历史
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
